package pt.zero.zero.model;

import model.IItem;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Created by nuno-rafael-figueiredo on 15-03-2014.
 */
public class Zapping {
    public static final String REFERRER = "http://www.google.com";
    public static final String URL = "http://www.zerozero.pt/zapping.php";
    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";

    public static final Entity ENTITY = new Entity();

    public static Entity getMap() {
        Entity map = new Entity();

        final Document document;
        try {
            document = Jsoup.connect(URL)
                    .userAgent(USER_AGENT)
                    .referrer(REFERRER)
                    .get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Element tbody = document
                .select("div.page > table > tbody > tr")
                .get(4)
                .select("table > tbody")
                .get(0);

        for (Element trs : tbody.select("tr.tr_lclaro")) {
            Elements tds = trs.select("td");
            Item item = new Item()
                    .setData(tds.get(0).text())
                    .setHora(tds.get(2).text())
                    .setCasa(tds.get(4).text())
                    .setFora(tds.get(6).text());

            Element img = tds.get(7).select("img").get(0);
            if (img != null)
                item.setTv(img.attr("tv"));

            item.setEdicao(tds.get(8).text());
            map.add(item);
        }
        return map;
    }

    public static class Entity extends model.KeyEntity<String, Item> {

        protected Entity() {
            super(Item[].class, Item.class);
        }

        @Override
        protected void parse() {
        }
    }

    /**
     * Created by nuno-rafael-figueiredo on 15-03-2014.
     */
    public static class Item implements IItem<String> {
        private String data;
        private String hora;
        private String casa;
        private String fora;
        private String tv;
        private String edicao;

        public String getData() {
            return data;
        }

        public String getHora() {
            return hora;
        }

        public String getCasa() {
            return casa;
        }

        public String getFora() {
            return fora;
        }

        public String getEdicao() {
            return edicao;
        }

        public String getTv() {
            return tv;
        }

        public Item setData(String data) {
            this.data = data;
            return this;
        }

        public Item setHora(String hora) {
            this.hora = hora;
            return this;
        }

        public Item setCasa(String casa) {
            this.casa = casa;
            return this;
        }

        public Item setFora(String fora) {
            this.fora = fora;
            return this;
        }

        public Item setTv(String tv) {
            this.tv = tv;
            return this;
        }

        public void setEdicao(String edicao) {
            this.edicao = edicao;
        }

        @Override
        public String getKey() {
            return casa + fora;
        }
    }
}
